package ${package.Controller};

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.entity.${entity};
import com.future.hair.server.mapper.${table.mapperName};
import com.future.hair.server.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.List;




/**
*
* @author ${author}
* @since ${date}
* @version v1.0
*/

@RestController
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/hair/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
@Api(tags = "${entity}管理接口")

public class ${table.controllerName}{
    @Autowired
    private ${table.mapperName} mapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @ApiOperation(value = "查询 ${entity} 列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult<List<${entity}>> ${table.entityPath}List(Integer current,Integer size,@RequestHeader String token) {
        if(current==null){
            current = 1;
        }
        if(size==null){
            size = 1;
        }
        Integer finalCurrent = current;
        Integer finalSize = size;
        return CommonResult.data(new RedisUtil<List<${entity}>>(redisTemplate).getObject("hair.${table.entityPath}.list",new RedisUtil.CallBack<List<${entity}>>() {
            @Override
            public List<${entity}> object() {
                return mapper.selectPage(new Page<>(current, size), null).getRecords();
            }
        }, List.class,${entity}.class));
    }

    @ApiOperation(value = "查询当前 ${entity}")
    @RequestMapping(value = "/id", method = RequestMethod.POST)
    public CommonResult<${entity}> ${table.entityPath}Id(int id,@RequestHeader String token) {
        return CommonResult.data(new RedisUtil<${entity}>(redisTemplate).getObject("hair.${table.entityPath}." + id, new RedisUtil.CallBack<${entity}>() {
            @Override
            public User object() {
                return mapper.selectById(id);
            }
        },${entity}.class));
    }

    @ApiOperation(value = "增加 ${entity}")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Integer> create(${entity} data,@RequestHeader String token) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("mobile", data.getMobile()); 请输入排异条件
        User user = mapper.selectOne(wrapper);
        if (user==null){
            Integer result = mapper.insert(data);
            if (result == 1){
                return CommonResult.data(result);
            }else {
                return CommonResult.failed("创建失败");
            }
        }
        return CommonResult.failed(ResultCode.USER_EXIST);
    }

    @ApiOperation(value = "更新 ${entity}")
    @RequestMapping(value = "/upData", method = RequestMethod.POST)
    public CommonResult<Integer> upData(${entity} data, @RequestHeader String token) {
        Integer result = mapper.updateById(data);
        if (result==1){
            return CommonResult.data(result);
        }else {
            return CommonResult.failed("更新失败");
        }
    }

    @ApiOperation(value = "删除 User")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult<Integer> delete(int id,@RequestHeader String token) {
        Integer result = mapper.deleteById(id);
        if (result==1){
            return CommonResult.data(result);
        }else {
            return CommonResult.failed("删除失败");
        }
    }
}